#!/usr/bin/env python
# encoding=utf-8

"""
插入一条数据（orgNo通过命令行参数传入）
"""

import sys

from elasticsearch import Elasticsearch
from pymongo import MongoClient
from pypinyin import lazy_pinyin
from scpy.logger import get_logger

reload(sys)
sys.setdefaultencoding('utf-8')

logger = get_logger(__file__)
MONGO_HOST = '10.132.23.104'
LIMIT = 5000
INDEX = 'company_info_20160325'
hosts = ['10.117.61.31', '10.162.81.30', '10.161.163.247']


def zh_2_py(string):
    return ' '.join(lazy_pinyin(string)).lower()


def main():
    es = Elasticsearch(hosts=hosts)
    conn = MongoClient(MONGO_HOST, 27017)
    coll = conn['organization']['companyNameStrict_2015_12']

    item = coll.find_one({'_id': sys.argv[1]})
    if not item:
        print 'not found'
        return
    chn = zh_2_py(item["companyName"])
    data = {
        "companyName": item["companyName"],
        "location": item["location"],
        "regNo": item["regNo"],
        "province": item["province"],
        "orgNo": item["_id"],
        "zfrq": item["zfrq"],
        "registOrg": item["registOrg"],
        "registTime": item["registTime"],
        'englishName': chn,
        'nameAlias': u'%s %s' % (item["companyName"], chn),
        'addressAlias': u'%s %s' % (item["companyName"], zh_2_py(item["location"])),
    }
    eng_item = conn['alibaba']['etoc_one'].find_one({'orgNo': data['orgNo']})
    if eng_item:
        data['englishName'] = eng_item['englishName']
    if not data['englishName']:
        data['englishName'] = chn
    es.index(INDEX, 'company-info', body=data, id=data['companyName'])


if __name__ == '__main__':
    main()
